package com.yanqu.road.dao.impl.channelcenter.tonbingchannel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingBarracks;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingPatrons;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingTeam;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelTonBingUserDataDaoImpl extends TempDataObjectDao<ChannelTonBingUserData> {

    public static String getChannelTonBingUserDataSql = "select * from `t_n_tonbing_user_data` where `activityId`=?";

    public Map<Long, ChannelTonBingUserData> getChannelTonBingUserDataMap(int activityId) {
        Map<Long, ChannelTonBingUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getChannelTonBingUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingUserData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateChannelTonBingUserDataSql = "update `t_n_tonbing_user_data` set " +
            "serverId=?,channelId=?,unionUid=?,campId=?,patronsMap=?,soldierAddition=?,teamMap=?,barracks=?," +
            "eventResetTime=?,eventTodayCount=?,eventMap=?,positionMap=?,posAddSilverMap=?," +
            "posAddSilverTime=?,zhanGong=?,zhanGongTime=?,needHelpTime=?,redDotMap=?,horseId=?," +
            "guide=?,totalPosSilver=?,zhanGongMap=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(ChannelTonBingUserData crossTonBingUserData) {
        crossTonBingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getChannelId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTonBingUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getSoldierAddition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getTeamMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getBarracks())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getEventResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getEventTodayCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getPositionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getPosAddSilverMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getPosAddSilverTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getZhanGongTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getNeedHelpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getRedDotMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getHorseId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getGuide()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTonBingUserData.getTotalPosSilver().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getZhanGongMap())));

        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getUserId()));
        if (getDb().executeNoneQuery(updateChannelTonBingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChannelTonBingUserDataSql = "insert into `t_n_tonbing_user_data`" +
            "(activityId,userId,unionUid,campId,patronsMap,soldierAddition,teamMap,barracks,eventResetTime," +
            "eventTodayCount,eventMap,positionMap,posAddSilverMap,posAddSilverTime,zhanGong,zhanGongTime,needHelpTime," +
            "redDotMap,horseId,guide,totalPosSilver,zhanGongMap,serverId,channelId) " +
            " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ChannelTonBingUserData crossTonBingUserData) {
        crossTonBingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTonBingUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getSoldierAddition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getTeamMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getBarracks())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getEventResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getEventTodayCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getPositionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getPosAddSilverMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getPosAddSilverTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getZhanGongTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getNeedHelpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getRedDotMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getHorseId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getGuide()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTonBingUserData.getTotalPosSilver().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingUserData.getZhanGongMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingUserData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingUserData.getChannelId()));

        if (getDb().executeNoneQuery(insertChannelTonBingUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ChannelTonBingUserData parseData(ResultSet rs) throws SQLException {
        ChannelTonBingUserData crossTonBingUserData = new ChannelTonBingUserData();
        crossTonBingUserData.setActivityId(rs.getInt("activityId"));
        crossTonBingUserData.setUserId(rs.getLong("userId"));
        crossTonBingUserData.setServerId(rs.getLong("serverId"));
        crossTonBingUserData.setChannelId(rs.getInt("channelId"));
        crossTonBingUserData.setUnionUid(rs.getString("unionUid"));
        crossTonBingUserData.setCampId(rs.getInt("campId"));
        crossTonBingUserData.setPatronsMap(JSON.parseObject(rs.getString("patronsMap"), new TypeReference<ConcurrentHashMap<Integer, ChannelTonBingPatrons>>() {
        }));
        crossTonBingUserData.setSoldierAddition(JSON.parseObject(rs.getString("soldierAddition"), new TypeReference<List<Long>>() {
        }));
        crossTonBingUserData.setTeamMap(JSON.parseObject(rs.getString("teamMap"), new TypeReference<ConcurrentHashMap<Integer, ChannelTonBingTeam>>() {
        }));
        crossTonBingUserData.setBarracks(JSON.parseObject(rs.getString("barracks"), ChannelTonBingBarracks.class));
        crossTonBingUserData.setEventResetTime(rs.getLong("eventResetTime"));
        crossTonBingUserData.setEventTodayCount(rs.getLong("eventTodayCount"));
        crossTonBingUserData.setEventMap(JSON.parseObject(rs.getString("eventMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
//        crossTonBingUserData.setPositionId(rs.getInt("positionId"));
        if(!StringUtils.isNullOrEmpty(rs.getString("positionMap"))){
            crossTonBingUserData.setPositionMap(JSON.parseObject(rs.getString("positionMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
            }));
        }
        crossTonBingUserData.setPosAddSilverMap(JSON.parseObject(rs.getString("posAddSilverMap"), new TypeReference<ConcurrentHashMap<Long, BigInteger>>() {
        }));
        crossTonBingUserData.setPosAddSilverTime(rs.getLong("posAddSilverTime"));
        crossTonBingUserData.setZhanGong(rs.getLong("zhanGong"));
        String str = rs.getString("zhanGongMap");
        if (!StringUtils.isNullOrEmpty(str)) {
            crossTonBingUserData.setZhanGongMap(JSON.parseObject(str, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }
        crossTonBingUserData.setZhanGongTime(rs.getLong("zhanGongTime"));
        crossTonBingUserData.setNeedHelpTime(rs.getLong("needHelpTime"));
        String redDotMap = rs.getString("redDotMap");
        if (!StringUtils.isNullOrEmpty(redDotMap)) {
            crossTonBingUserData.setRedDotMap(JSON.parseObject(redDotMap, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }
        crossTonBingUserData.setHorseId(rs.getInt("horseId"));
        crossTonBingUserData.setGuide(rs.getInt("guide"));
        String totalPosSilver = rs.getString("totalPosSilver");
        if (!StringUtils.isNullOrEmpty(totalPosSilver)) {
            crossTonBingUserData.setTotalPosSilver(new BigInteger(totalPosSilver));
        }
        return crossTonBingUserData;
    }

}
